package com.group02.community.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group02.community.entity.vo.RepairVO;
import com.group02.community.mapper.RepairMapper;
import com.group02.community.service.IRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper,RepairVO> implements IRepairService {
   @Autowired
   private RepairMapper repairMapper;

    @Override
    public IPage<RepairVO> getRepairPage(Long current, Long size, RepairVO repairVO) {
        Page<RepairVO> page = new Page<>(current, size);
        page.setOptimizeCountSql(false);
        return repairMapper.selectRepairPage(page,repairVO);
    }

    @Override
    public void addRepair(RepairVO repairVO) {
        repairMapper.addRepair(repairVO);
    }

    @Override
    public void addOwner(RepairVO repairVO) {
        repairMapper.addOwner(repairVO);
    }

    @Override
    public void updateRepair(RepairVO repairVO) {
        repairMapper.updateRepair(repairVO);
    }

    @Override
    public void downloadExcel(HttpServletResponse response) throws IOException {
        List<RepairVO> repairVOS= repairMapper.selectAll();
        //设置文件下载
        //设置响应头，告诉浏览器要以文件的形式保存
//        response.setHeader();
        response.setHeader("content-disposition","attachment;filename="+System.currentTimeMillis()+".xlsx");
        //生成Excel并写入
        EasyExcel.write(response.getOutputStream(), RepairVO.class).sheet(System.currentTimeMillis()+"").doWrite(repairVOS);
    }

    @Override
    public List<RepairVO> selectAll() {
        return repairMapper.selectAll();
    }

    @Override
    public List<RepairVO> selectOwner(Long ownerId) {
        return repairMapper.selectOwner(ownerId);
    }
}
